package com.axend.aerosense.room.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.common.adapter.ContentSelecteListAdapter;
import com.axend.aerosense.room.ui.activity.SaveModelActivity;
import com.axend.aerosense.room.viewmodel.RoomTypeViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.axend.aerosense.base.app.BaseApplication;
import com.axend.aerosense.base.bean.RoomTypeBean;
import com.axend.aerosense.base.database.room.RoomTypeMananger;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.IToolBarTextClickListence;
import com.axend.aerosense.common.bean.RoomTypeInfoBean;
import com.axend.aerosense.room.BR;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentTypeListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RoomTypeListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RoomTypeListFragment extends MvvmBaseFragment<RoomFragmentTypeListBinding, RoomTypeViewModel>
        implements IToolBarTextClickListence, ILoadDataResult<RoomTypeInfoBean> {
    private ArrayList<RoomTypeBean> roomList = null;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ROOM_TYPE = "ROOM_TYPE";
    private ContentSelecteListAdapter adapter;

    // TODO: Rename and change types of parameters
    private RoomTypeBean oldRoomType;
    private RoomTypeBean selectRoomType;
    public RoomTypeListFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param oldRoomType Parameter 1.
     * @return A new instance of fragment RoomNameFragment.
     */
    // TODO: Rename and change types and number of parameter
    public static RoomTypeListFragment newInstance(RoomTypeBean oldRoomType) {
        RoomTypeListFragment fragment = new RoomTypeListFragment();
        Bundle args = new Bundle();
        args.putSerializable(ROOM_TYPE, oldRoomType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            oldRoomType = (RoomTypeBean) getArguments().getSerializable(ROOM_TYPE);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        adapter = new ContentSelecteListAdapter();
        viewDataBinding.roomTypeList.setLayoutManager(new LinearLayoutManager(this.getContext()));
        viewDataBinding.roomTypeList.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<RoomTypeBean> list = (List<RoomTypeBean>)adapter.getData();
                for(RoomTypeBean tmp: list){
                    tmp.isSelect = false;
                }
                list.get(position).isSelect = true;
                selectRoomType = list.get(position);
                adapter.notifyDataSetChanged();
            }
        });
        setLoadSir(viewDataBinding.roomLoading);
        showLoading();
        viewModel.onLoad();
    }


    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_type_list;
    }

    @Override
    public int getBindingVariable() {
        return BR.roomNameModel;
    }

    @Override
    protected RoomTypeViewModel getViewModel() {
        BaseViewModelFactroy<RoomTypeInfoBean> myViewModelFactroy = new BaseViewModelFactroy<RoomTypeInfoBean>(this, this);
        return new ViewModelProvider(this, myViewModelFactroy).get(RoomTypeViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void onSure() {
        Intent intent = new Intent();
        intent.putExtra(SaveModelActivity.OPEN_MODE_ROOM_TYPE, selectRoomType);
        getActivity().setResult(SaveModelActivity.REQUEST_CODE_SET_TYPE, intent);
        getActivity().finish();
    }

    @Override
    public void onCancel() {
        getActivity().finish();
    }

    @Override
    public void loadDataSuccess(RoomTypeInfoBean roomTypeInfoBean) {
        showContent();
        roomList = BaseApplication.getInstance().getRoomTypes();
        roomList.clear();
        roomList.addAll(roomTypeInfoBean.getRoomInfo());
        RoomTypeMananger.getInstance(getActivity().getApplicationContext()).roomDao().insertList(roomList);
        for(RoomTypeBean tmp: roomList){
            if(tmp.getRoomType() == oldRoomType.getRoomType()){
                tmp.isSelect = true;
            }
        }
        adapter.setList(roomList);
    }

    @Override
    public void loadDataFail(String decs) {
        showEmpty();
    }
}